<?php
/**
 * Created by PhpStorm.
 * User: xiaomeng
 * Date: 17-9-14
 * Time: 下午3:39
 */

namespace swf\socket;

use swf\core\syncDb;

class OperaBet
{
    /**
     *
     */
//    public $betting = [
//        1=>[2=>100,3=>300,5=>500,12=>1200,25=>100],
//        2=>[2=>100,3=>300,5=>500,12=>1200,25=>100],
//        ];

    /**
     * 返回本房间的投注详情
     * @param $room_id
     * @param $num
     * @param $betMoney
     */
    static public function addBet($room_id,$num,$betMoney,$no_id)
    {
        $bet_list = syncDb::redis()->get('bet_list');
        $bet_list = json_decode($bet_list,true);
        if(self::isUpdate($no_id))
        {
            @$bet_list[$room_id][$num] += $betMoney;
            syncDb::redis()->set('bet_list',json_encode($bet_list));
        }
        return $bet_list[$room_id];
    }

    /**
     * 返回本房间的投注详情
     * @param $room_id
     * @param $del_bet
     * @return mixed
     */
    static public function delBet($room_id,$del_bet,$no_id)
    {
        $bet_list = syncDb::redis()->get('bet_list');
        $bet_list = json_decode($bet_list,true);
        if(self::isUpdate($no_id))
        {
            foreach ($del_bet as $k=>$v)
            {
                @$bet_list[$room_id][$k] -= $v;
            }
            syncDb::redis()->set('bet_list',json_encode($bet_list));
        }
        return $bet_list[$room_id];
    }

    /**
     * 返回本房间的投注详情
     * @param $room_id
     */
    static public function getRoomBet($room_id)
    {
        $bet_list = syncDb::redis()->get('bet_list');
        $bet_list = json_decode($bet_list,true);
        $res = isset($bet_list[$room_id])?$bet_list[$room_id]:[];
        return $res;
    }

    /**
     * 清空投注
     * @return mixed
     */
    static public function clearBet()
    {
        return syncDb::redis()->del('bet_list');
    }

    /**
     * 获取当前旗号
     */
    static public function isUpdate($no_id)
    {
        $game_status = syncDb::redis()->get('game_status');
        $game_status = json_decode($game_status,true);


//        && $game_status['game_status']['open_status'] != 4
        if($game_status['game_status']['no_id'] == $no_id)
        {
            return true;
        }
        return false;
    }
}